﻿@namespace AIStudio.BlazorUI.Pages.User
@layout UserLayout
@page "/Home/Login"
@inject IJSRuntime JS

@using Blazored.LocalStorage

<div class="main__b__0">
    <div class="login">
        <Form Model="@_model" Loading="loading" OnFinish="HandleSubmit">
            <Tabs ActiveKey="@context.LoginType">
                <TabPane Key="1" Tab="Account Login">
                    <FormItem>
                        <AntDesign.Input Placeholder="Username: Admin or Alice" Size="large" @bind-Value="@context.UserName">
                            <Prefix><Icon Type="user" /></Prefix>
                        </AntDesign.Input>
                    </FormItem>
                    <FormItem>
                        <AntDesign.Input Placeholder="Password: Admin or 123456" Size="large" @bind-Value="@context.Password" Type="password">
                            <Prefix><Icon Type="lock" /></Prefix>
                        </AntDesign.Input>
                    </FormItem>
                </TabPane>
            </Tabs>
            <div>
                <Checkbox @bind-Checked="@context.AutoLogin">
                    Auto Login
                </Checkbox>
            </div>
            <Button Type="primary" HtmlType="submit" Class="submit" Size="large" Block>Log in </Button>
        </Form>
    </div>
</div>

@code {
    private readonly LoginInputDTO _model = new LoginInputDTO();
    bool loading = false;

    [Inject] 
    public NavigationManager NavigationManager { get; set; }

    [Inject] 
    public JwtAuthenticationStateProvider AuthStateProvider { get; set; }

    [Inject]
    ILocalStorageService StorageService { get; set; }

    [Inject] 
    public MessageService Message { get; set; }

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();

        var loginParamsType = await StorageService.GetItemAsStringAsync(nameof(LoginInputDTO));
        if (!string.IsNullOrEmpty(loginParamsType))
        {
            var temp = loginParamsType.ToObject<LoginInputDTO>();

            temp.CopyProperties(_model);
        }
    }

    private IJSObjectReference? module;

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            module = await JS.InvokeAsync<IJSObjectReference>("import", "./_content/AIStudio.BlazorUI/js/md5.js");
        }
    }

    public async void HandleSubmit()
    {
        try
        {
            loading = true;
            var password = _model.Password;
            password = await module.InvokeAsync<string>("hex_md5", password); 
            var result = await AuthStateProvider.Login(_model.UserName, password);
            if (result.Success)
            {           
                if (_model.AutoLogin)
                {
                    await StorageService.SetItemAsStringAsync(nameof(LoginInputDTO), _model.ToJson());
                }
                else
                {
                    await StorageService.RemoveItemAsync(nameof(LoginInputDTO));
                }
                NavigationManager.NavigateTo("/");
                await Message.Success("登录成功");
            }
            else
            {
                loading = false;
                await Message.Warning(result.Msg);
            }
        }
        catch (Exception ex)
        {
            await Message.Error(ex.Message);
        }
        finally
        {
          
        }
    }
}